// 这个文件是头文件和源文件混合在一起
#pragma once
#include<iostream>
#include<unistd.h>
using namespace std;
typedef void(*work_t)(int); // 函数指针

typedef void(*task_t)(int,pid_t); // 函数指针

void PrintLog(int fd,pid_t id)
{
    cout<<"sub Process:"<< id << "fd: " << fd <<" Task is PrintLog() Execute!\n"<<endl;
}

void ReLoadConf(int fd,pid_t id)
{
    cout<<"sub Process:"<< id << "fd: " << fd <<" Task is ReLoadConf Execute!!\n"<<endl;
}
void ConnectMySql(int fd,pid_t id)
{
    cout<<"sub Process:"<< id << "fd: " << fd <<" Task is Connect MySql!\n"<<endl;
}
// 创建任务表
task_t task[3] = {PrintLog,ReLoadConf,ConnectMySql};

uint32_t NextTask()
{
    uint32_t command_code = rand()%3;
    return command_code;
}

void worker(int fd)
{
    // 从0中读取任务即可.
    while(true)
    {
        // 指令代码 从管道中读取命令代码.
        uint32_t command_code = 0;
        ssize_t n = read(0,&command_code,sizeof(command_code));
        if(n == sizeof(command_code))
        {
            if(command_code >= 3) continue;
            task[command_code](fd,getpid());
        }
        sleep(1);
    }
}